//
//  WBNavigationViewController.swift
//  小荣荣新浪微博
//
//  Created by zzr on 2018/4/19.
//  Copyright © 2018年 zzr. All rights reserved.
//

import UIKit

class WBNavigationViewController: UINavigationController {

    override func viewDidLoad() {
        //隐藏navigationBar
        navigationBar.isHidden = true
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //不是栈底控制器才需要隐藏，根控制器不需要处理
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            if let vc = viewController as? WBBaseViewController {
                
                var title = "返回"
                //判断控制器级数,只有一个自控制器的时候，显示栈底控制器的标题
                if childViewControllers.count == 1 {
                    //title显示首页的标题
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                //取出navItem
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBackButton: true)
            }
        }
        
        
        super.pushViewController(viewController, animated: true)
    }
    
    //返回到上一级控制器
    @objc private func popToParent() {
        popViewController(animated: true)
    }

}
